﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TomNet.IMSC.Core.Contracts.Common;
using TomNet.IMSC.Core.Contracts.Organization;
using TomNet.IMSC.Model.DbModels.Common;
using TomNet.Web.Mvc;
using TomNet.Web.Mvc.UI;

namespace TomNet.IMSC.Web.Controllers.QandA
{
    [Description("内部备注")]
    public class CommonRemarkController : BaseController
    {
        // GET: CommonRemark
        public ICommonRemarkContract CommonRemarkContract { get; set; }
        public IStaffContract StaffContract { get; set; }
        #region 列表
        [Description("内部备注-列表")]
        public ActionResult Index(int GroupID=0,int InterfixID = 0)
        {
            ViewBag.GroupID = GroupID;
            ViewBag.InterfixID = InterfixID;
            return View();
        }
        [HttpPost]
        public ActionResult IndexAsync(int pageIndex, int pageSize, string search = "", int GroupID = 0, int InterfixID = 0)
        {
            var query = from c in CommonRemarkContract.Entities
                        select new
                        {
                            Id = c.Id,
                            GroupID = c.GroupID,
                            InterfixID = c.InterfixID,
                            StaffID = (from s in StaffContract.Entities where s.IsDeleted == false && c.StaffID == s.Id select s.Name).FirstOrDefault(),
                            Remark = c.Remark,
                            CreateTime = c.CreateTime 
                        };

            if (!string.IsNullOrWhiteSpace(search))
            {
                query = query.Where(m => m.Remark.Contains(search));
            }

            if (GroupID != 0)
            {
                query = query.Where(m => m.GroupID == GroupID);
            }

            if (InterfixID != 0)
            {
                query = query.Where(m => m.InterfixID == InterfixID);
            }

            //数据总数
            var total = query.Count();

            //默认给一个排序的字段
            query = query.OrderByDescending(m => m.Id);

            //分页(假如total == 0，则取消分页查询，提高性能)
            query = total > 0 ? query.Skip((pageIndex - 1) * pageSize).Take(pageSize)
                : query;

            //此处可以采用匿名对象 GridData<object>
            GridData<object> gridData = new GridData<object>() { Total = total, Rows = query.ToList() };

            //此处采用重写后的jsonResult
            return JsonEx(gridData, JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm:ss");
        }
        #endregion
         
        #region 添加
        [Description("广告-添加")]
        public ActionResult Insert(int InterfixID=0)
        {
            ViewBag.InterfixID = InterfixID;
            return View();
        }
        [HttpPost]
        public ActionResult InsertAsync(CommonRemark entity)
        { 
            var cookies = Common.CookiesManagement.GetCookies();
            var ticket = Common.CookiesManagement.GetTicket(cookies);
            var model = Common.CookiesManagement.GetLoginModel(ticket);
            var datatime = DateTime.Now;
            entity.GroupID = 1;
            entity.CreateTime = datatime;

            entity.StaffID = model.Id;
            //所有AJAX的结果，返回统一数据格式
            var result = new AjaxResult();
            try
            {
                var count = CommonRemarkContract.Insert(entity);
                if (count > 0)
                {
                    result.Type = AjaxResultType.Success;
                    result.Content = "录入成功";
                }
                else
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = "录入失败";
                }
            }
            catch
            {
                result.Type = AjaxResultType.Error;
                result.Content = "异常操作";
            }
            return JsonEx(result);
        }
        #endregion
         
        #region 修改
        public ActionResult Edit(int id)
        { 
            var entity = CommonRemarkContract.GetByKey(id);
            //后台容错，有异常数据直接抛出。框架会自动跳转到错误页面。
            if (entity == null) throw new Exception("未找到该实体");
            ViewBag.entity = entity;
            return View();
        }
        [HttpPost]
        public ActionResult EditAsync(CommonRemark entity)
        {
            //所有AJAX的结果，返回统一数据格式
            var result = new AjaxResult();

            var cookies = Common.CookiesManagement.GetCookies();
            var ticket = Common.CookiesManagement.GetTicket(cookies);
            var model = Common.CookiesManagement.GetLoginModel(ticket); 
            var staffid = model.Id;

            var datatime = DateTime.Now;
            entity.GroupID = 1;
            entity.CreateTime = datatime;
             
            
            try
            {
                if (staffid != entity.StaffID)
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = "不得修改他人备注";
                }
                else
                {
                    var count = CommonRemarkContract.Update(entity);
                    if (count > 0)
                    {
                        result.Type = AjaxResultType.Success;
                        result.Content = "修改成功";
                    }
                    else
                    {
                        result.Type = AjaxResultType.Error;
                        result.Content = "修改失败";
                    }
                }
            }
            catch
            {
                result.Type = AjaxResultType.Error;
                result.Content = "异常操作";
            }
            return JsonEx(result);
        }
        #endregion

        #region 删除
        [Description("广告-删除")]
        [HttpPost]
        public ActionResult DeleteAsync(string ids)
        {
            var result = new AjaxResult();
            try
            {
                string[] idsStr = ids.Split(new char[] { ',' });
                int[] idsInt = Array.ConvertAll(idsStr, id => Convert.ToInt32(id));
                foreach (var item in idsInt)
                {
                    CommonRemarkContract.DeleteDirect(item);
                }

                result.Type = AjaxResultType.Success;
                result.Content = "删除成功";
            }
            catch
            {
                result.Type = AjaxResultType.Error;
                result.Content = "异常操作";
            }
            return JsonEx(result);
        }
        #endregion 
    }
}